<script setup>
import { ref,onMounted } from 'vue'
import { queryWarehousingEntryDetalis,confirmWarehousingEntryDetalis } from '../../api/warehousingentrydetails'
import { ElMessage } from 'element-plus'

//查询的点击事件
const query = async ()=>{ 
  const result = await queryWarehousingEntryDetalis(page.value,pageSize.value,warehousingEntryDetailsFrom.value.supplierName)
  warehousingEntryDetailsList.value = result.data.rows
  total.value = result.data.total
}

//定义表格数据
const warehousingEntryDetailsList = ref([])
//定义表单数据
const warehousingEntryDetailsFrom = ref({supplierName:''})
//清空表单数据
const clear = ()=>{
  warehousingEntryDetailsFrom.value = {supplierName:''}
  query()
}
//定义弹出框数据
const title = ref(null)
const dialogShow = ref(false)
const dialogData = ref({realArrivalTime:''})

//定义分页的变量
const page = ref(1)
const pageSize = ref(10)
const total = ref(0)
//定义分页的点击事件
const handleSizeChange = (val) => {
  page.value = 1
  query()
}
const handleCurrentChange = (val) => {
  query()
}
//创建一个id集合来接收
const idsList = ref([])
//定义表格的选中事件
const handleSelectionChange = (value) => {
  idsList.value = value.map(item=>item.id)
}
//挂载
onMounted(() => {
  query()
  // supplierAllList()
})


//提交的点击事件
const saveWarehousingEntryDetailsData = ()=>{ 
  // 检查是否有选中的数据
  if (idsList.value.length === 0) {
    ElMessage.warning('请先选择要操作的数据！')
    return
  }
  title.value = '确定到货时间'
  dialogShow.value = true
  // 清空表单
  dialogData.value = {realArrivalTime:''}
}
//定义存数据的变量
const data = ref({dateTime:'',list:[]})

//确定按钮的点击事件
const confirmeWarehousingEntryDetailsInfo = async () => { 
  data.value.list = idsList.value
  data.value.dateTime = dialogData.value.realArrivalTime
  const result = await confirmWarehousingEntryDetalis(data.value)
  if(result.code){
    ElMessage.success('操作成功！')
    dialogShow.value = false
    idsList.value = []
    query()
  }else{
    ElMessage.error('操作失败！请重新操作')
  }
}
</script>

<template>
<div><h4>待到货</h4></div>
<div>
  <el-form :inline="true" :model="warehousingEntryDetailsFrom" class="demo-form-inline">
    <el-form-item>
      <el-button type="primary" @click="saveWarehousingEntryDetailsData" circle plain><el-icon><Select /></el-icon></el-button>
    </el-form-item>
    <el-form-item>
      <el-input v-model="warehousingEntryDetailsFrom.supplierName" placeholder="供应商名称" style="width: 200px;"/>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="query" plain>查询</el-button>
      <el-button type="default" @click="clear" plain>清空</el-button>
    </el-form-item>
  </el-form>
</div>

<!-- table表格 -->
<div>
  <el-table :data="warehousingEntryDetailsList" @selection-change="handleSelectionChange" border style="width: 100%" >
    <el-table-column type="selection" width="55" />
    <el-table-column fixed type="index" label="序号" align="center"  width="80"/>
    <el-table-column prop="batchNumber" label="批次号" align="center" width="120"/>
    <el-table-column prop="supplierName" label="供应商名称" align="center" width="180"/>
    <el-table-column prop="title" label="商品名称" align="center" width="200"/>
    <el-table-column prop="count" label="商品数量" align="center" width="120"/>
    <el-table-column prop="price" label="商品价格" align="center" width="120"/>
    <el-table-column prop="consignorName" label="货主名称" align="center"  width="200"/>
    <el-table-column prop="predictedArrivalTime" label="预测到达时间" align="center" width="200"/>
  </el-table>
</div>


<!-- 分页 -->
<div>
  <el-pagination
  v-model:current-page="page"
  v-model:page-size="pageSize"
  :page-sizes="[5, 10, 15, 20]"
  layout="total, sizes, prev, pager, next, jumper"
  :total="total"
  @size-change="handleSizeChange"
  @current-change="handleCurrentChange"/>
</div>

<!-- 弹出框 -->
<div>
  <el-dialog v-model="dialogShow" :title="title" width="500" align-center>
    <el-form :model="dialogData">
      <el-form-item>
        <el-date-picker v-model="dialogData.realArrivalTime" type="date" placeholder="实际到达时间" value-format="YYYY-MM-DD" style="width: 2000px;" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="dialogShow = false" type="default">取消</el-button>
      <el-button type="primary" @click="confirmeWarehousingEntryDetailsInfo" plain>确定</el-button>
    </template>
  </el-dialog>
</div>
</template>
<style scoped>
</style>